package tj.warning.pull.config;

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration
@MapperScan(basePackages = {"tj.warning.pull.mapper.report2"}, sqlSessionFactoryRef = "sqlSessionFactoryReport2")
public class Report2MybatisConfiguration {

    @Bean(name = "report2DB")
    @ConfigurationProperties(prefix = "spring.datasource.report2")
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactoryReport2")
    public SqlSessionFactory sqlSessionFactoryReport2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource2());

        factoryBean.setConfiguration(new MybatisProperties().getConfiguration());
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/report2/*.xml"));
        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateReport2() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryReport2());
    }

}
